package com.ymatou.iapi.dglogin.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.ymatou.iapi.dglogin.parameter.DistributorRegisterBean;
import com.ymatou.iapi.dglogin.service.DistributorRegisterCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.model.DGUserExtInfo;
import com.ymttest.database.model.DGUserInfo;
import com.ymttest.database.model.DGWxUnionidBindInfo;
import com.ymttest.database.model.godmember.Member;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
import com.ymttest.utils.TestDataManager;
/**
*分销员注册并联登
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DistributorRegister{
	private static DistributorRegisterBean distributorregisterBean;
	private static DistributorRegisterCall distributorregisterCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分销员注册并联登");
	}

	@Before
	public void caseUp() {
		distributorregisterBean=new DistributorRegisterBean();
		distributorregisterCall=new DistributorRegisterCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(distributorregisterCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private DistributorRegisterBean initRegBean() {
		DistributorRegisterBean distributorregisterBean2=new DistributorRegisterBean();
		distributorregisterBean2.setAppSource(60);
		distributorregisterBean2.setClientIp(TestDataManager.getRandomIp());
		distributorregisterBean2.setCountryCode("China");
		distributorregisterBean2.setMobile(TestDataManager.getPhoneNum());
		distributorregisterBean2.setMobileAreaCode("+86");
		distributorregisterBean2.setPassword("123456a");
		distributorregisterBean2.setShopId(1);
		distributorregisterBean2.setTerminalSource(1);
		distributorregisterBean2.setWxAppName(DGUserCallService.TestWxsqSeller);
		return distributorregisterBean2;
	}
	private void verifyDB(DistributorRegisterBean usercreateBean2, Integer uid) {
		Logger.comment("验证DGUserInfo");
		DGUserWapper DGUserWapper=new DGUserWapper();
		DGUserInfo uinfo = DGUserWapper.selectDGUserInfo(uid).get(0);
		Logger.debug("uinfo:"+FormatUtil.beanToGSONString(uinfo));
//		Logger.verifyEquals(usercreateBean2.getIscs() == null ? false : usercreateBean2.getIscs(), uinfo.getIsCs(), "验证IsCs");
//		Logger.verifyEquals(usercreateBean2.getLoginId(), uinfo.getLoginId(), "验证LoginId");
		Logger.verifyNotNull(uinfo.getLoginPwd(), "验证LoginPwd");
//		Logger.verifyEquals(usercreateBean2.getLogoUrl(), uinfo.getLogoUrl(), "验证LogoUrl");
		if (usercreateBean2.getMobile() != null) {
			Logger.verifyEquals(usercreateBean2.getMobile(), uinfo.getMobile(), "验证Mobile");
		}
		if (usercreateBean2.getMobileAreaCode() != null) {
			Logger.verifyEquals(usercreateBean2.getMobileAreaCode(), uinfo.getMobileAreaCode(), "验证MobileAreaCode");
		}
//		if (usercreateBean2.getBelongSellerId() != null) {
//			Logger.verifyEquals(usercreateBean2.getBelongSellerId(), uinfo.getBelongSellerId(), "验证BelongSellerId");
//		}
		if (usercreateBean2.getShopId() != null) {
			Logger.verifyEquals(usercreateBean2.getShopId(), uinfo.getShopId(), "验证getShopId");
		}
		Logger.verifyEquals(true, uinfo.getIsSetPwd(), "验证IsSetPwd");
//		Logger.verifyEquals(usercreateBean2.getNickname(), uinfo.getNickname(), "验证Nickname");
//		Logger.verifyEquals(usercreateBean2.getUserGender()==null?0:usercreateBean2.getUserGender(), uinfo.getUserGender(), "验证UserGender");
		Logger.verifyEquals(3, uinfo.getUserType(), "验证UserType");
		Logger.verifyEquals(0, uinfo.getUserStatus(), "验证UserStatus");
		Logger.comment("验证DGUserExtInfo");
		DGUserExtInfo exinfo = DGUserWapper.selectDGUserExtInfo(uid).get(0);
		Logger.debug("exinfo:"+FormatUtil.beanToGSONString(exinfo));
		Logger.verifyEquals(usercreateBean2.getAppSource(), exinfo.getAppSource(), "验证AppSource");
		if (usercreateBean2.getClientIp() != null) {
			Logger.verifyEquals(usercreateBean2.getClientIp(), exinfo.getClientIp(), "验证ClientIp");
		}
		Logger.verifyEquals(usercreateBean2.getCountryCode(), exinfo.getCountryCode(), "验证CountryCode");
		Logger.verifyEquals(usercreateBean2.getTerminalSource()==null?0:usercreateBean2.getTerminalSource(), exinfo.getTerminalSource(), "验证TerminalSource");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorRegister_001() {
		Logger.start(true,"分销员注册并联登成功");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("true", distributorregisterCall.getString("success"), "验证success");
			Logger.verifyNotNull(distributorregisterCall.getString("accessToken"), "验证accessToken");
			Integer userId=Integer.valueOf(distributorregisterCall.getString("userId"));
			verifyDB(distributorregisterBean, userId);
			 MessageListBean MessageListBean = MqQueryCallService.initDistributorRegister(userId);
			 List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(true, joa.size()>=1, "验证size");
			//{"dsId":501103745,"mobile":"13640065631","shopId":500001106}
			Logger.verifyEquals( userId.toString(),joa.get(0).get("dsId").toString(), "验证userId");
			Logger.verifyEquals(distributorregisterBean.getShopId().toString(),joa.get(0).get("shopId").toString(), "验证shopId");
			Logger.verifyEquals(distributorregisterBean.getMobile(),joa.get(0).get("mobile").toString(), "验证mobile");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_002() {
		Logger.start(true,"存在同手机号会员,分销员注册后绑定成功");
		try {
			Integer shopid = DGUserCallService.TestWxsqOtherSellerId;
			//开启邀请制
            GodMemberService.setCollectionMobile(shopid, true);
            GodMemberService.setInvitationCodeCall(shopid, true, 0);
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			//绑定了手机 返回不需要邀请码
			String mobile=TestDataManager.getPhoneNum();
			GodMemberService.bindMobile(shopid, userId, mobile);
			distributorregisterBean=initRegBean();
			distributorregisterBean.setShopId(shopid);
			distributorregisterBean.setWxAppName(shopid.toString());
			distributorregisterBean.setMobile(mobile);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();
			Logger.verifyEquals("true", distributorregisterCall.getString("success"), "验证success");
			Logger.verifyNotNull(distributorregisterCall.getString("accessToken"), "验证accessToken");
			Integer userId2=Integer.valueOf(distributorregisterCall.getString("userId"));
			Thread.sleep(3000);
			Member mem =new GodMemberWapper().selectMemberByMemberid(memid).get(0);
			Logger.verifyEquals(userId2, mem.getDsId(),"验证dsid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_003() {
		Logger.start(true,"不输入shopId失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterBean.setShopId(null);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_004() {
		Logger.start(true,"不输入mobile失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterBean.setMobile(null);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_005() {
		Logger.start(true,"不输入mobileAreaCode失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterBean.setMobileAreaCode(null);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_006() {
		Logger.start(true,"不输入CountryCode失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterBean.setCountryCode(null);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_007() {
		Logger.start(true,"不输入appSource失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterBean.setAppSource(null);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_008() {
		Logger.start(true,"不输入terminalSource-默认0");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterBean.setTerminalSource(null);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("true", distributorregisterCall.getString("success"), "验证success");
			Logger.verifyNotNull(distributorregisterCall.getString("accessToken"), "验证accessToken");
			Integer userId=Integer.valueOf(distributorregisterCall.getString("userId"));
			verifyDB(distributorregisterBean, userId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_009() {
		Logger.start(true,"不输入password失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterBean.setPassword(null);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_010() {
		Logger.start(true,"不输入wxAppName失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterBean.setWxAppName(null);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_011() {
		Logger.start(true,"同一个店铺中mobile已经被使用失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();				
			distributorregisterCall.callService();				
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_012() {
		Logger.start(true,"不同店铺(belongsellerid)中mobile已经被使用可以成功");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("true", distributorregisterCall.getString("success"), "验证success");
			Logger.verifyNotNull(distributorregisterCall.getString("accessToken"), "验证accessToken");
			Integer userId=Integer.valueOf(distributorregisterCall.getString("userId"));
			verifyDB(distributorregisterBean, userId);
			distributorregisterBean.setWxAppName(DGUserCallService.TestWxsqOtherSellerId.toString());
			distributorregisterCall.callService();	
			Logger.verifyEquals("true", distributorregisterCall.getString("success"), "验证success");
			Logger.verifyNotNull(distributorregisterCall.getString("accessToken"), "验证accessToken");
			userId=Integer.valueOf(distributorregisterCall.getString("userId"));
			verifyDB(distributorregisterBean, userId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DistributorRegister_013() {
		Logger.start(true,"belongsellerid相同shopid不同mobile已经被使用失败");
		try {
			distributorregisterBean=initRegBean();
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			distributorregisterBean.setShopId(1);
			distributorregisterCall.callService();	
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorRegister_014() {
		Logger.start(true,"分销员注册并联登成功-存在App和h5-2个渠道会员");
		try {
			Integer shopid = DGUserCallService.TestWxsqOtherSellerId;
			//开启邀请制
            GodMemberService.setCollectionMobile(shopid, true);
            GodMemberService.setInvitationCodeCall(shopid, true, 0);
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			UserCreateBean usercreateBean2= DGUserCallService.initUsercreateBean();
			int userId2=DGUserCallService.UserCreateCall(usercreateBean2);
			String mobile=TestDataManager.getPhoneNum();
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			//60.小程序 61.h5
			createMem.setAccessSource(0);
			String memid =GodMemberService.creatememberCall(createMem);
			
			CreateMemberBean createMem2 = GodMemberService.initCreateMemberBean(shopid,userId2,usercreateBean2.getOpenId());
			//60.小程序 61.h5
			createMem2.setAccessSource(1);
			String memid2 = GodMemberService.creatememberCall(createMem2);
			
			GodMemberService.bindMobile(shopid, userId, mobile);
			GodMemberService.bindMobile(shopid, userId2, mobile);
			
			distributorregisterBean=initRegBean();
			distributorregisterBean.setShopId(shopid);
			distributorregisterBean.setWxAppName(shopid.toString());
			distributorregisterBean.setMobile(mobile);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();
			Logger.verifyEquals("true", distributorregisterCall.getString("success"), "验证success");
			Logger.verifyNotNull(distributorregisterCall.getString("accessToken"), "验证accessToken");
			Integer userId3=Integer.valueOf(distributorregisterCall.getString("userId"));
			Thread.sleep(3000);
			Member mem =new GodMemberWapper().selectMemberByMemberid(memid).get(0);
			Logger.verifyEquals(userId3, mem.getDsId(),"验证dsid");
			
			mem =new GodMemberWapper().selectMemberByMemberid(memid2).get(0);
			Logger.verifyEquals(userId3, mem.getDsId(),"验证dsid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorRegister_015() {
		Logger.start(true,"分销员注册不能使用商家手机号");
		try {
			String mobile=TestDataManager.getPhoneNum();
			int cseller = createSeller(mobile);
			distributorregisterBean=initRegBean();
			distributorregisterBean.setWxAppName(DGUserCallService.TestWxAppName);
			distributorregisterBean.setMobile(mobile);
			distributorregisterCall.setData(distributorregisterBean);
			distributorregisterCall.callService();			
			Logger.verifyEquals("false", distributorregisterCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	public static int createSeller(String mobile) throws Exception {

		UserCreateBean u = DGUserCallService.initUsercreateBean();
		u.setIscs(true);
		u.setUserType(1);
		u.setAppSource(DGUserCallService.DG_SellerWxApp);
		u.setWxAppName(DGUserCallService.TestWxAppName);
		u.setMobile(mobile);
		u.setMobileAreaCode("+86");
		Integer sellerId = DGUserCallService.UserCreateCall(u);

		return sellerId;
	}
}